package _struct

import "fmt"

type Student struct {
	id    int
	name  string
	age   int
	score float64
}

func NewStudent(id int, name string, age int, score float64) *Student {
	return &Student{id, name, age, score}
}

type Stus struct {
	students []Student
}

func (s *Stus) init() {
	s.students = make([]Student, 0)
}

func (s *Stus) print() {
	for _, stu := range s.students {
		fmt.Println(stu)
	}

	fmt.Println()
}

func (s *Stus) add(stu *Student) {
	s.students = append(s.students, *stu)
}

func (s *Stus) update(stu *Student) {
	for i := 0; i < len(s.students); i++ {
		if s.students[i].id == stu.id {
			s.students[i] = *stu
		}
	}
}

func (s *Stus) delete(id int) {
	index := -1
	for i := 0; i < len(s.students); i++ {
		if s.students[i].id == id {
			index = i
		}
	}

	if index >= 0 {
		stus := s.students

		stus2 := append(stus[:index], stus[index+1:]...)
		s.students = stus2
	}
}

func TestStu() {
	s1 := NewStudent(1, "1", 18, 6.5)
	s2 := NewStudent(2, "2", 18, 6.6)
	s3 := NewStudent(3, "3", 18, 6.7)

	var stus Stus
	stus.init()
	stus.add(s1)
	stus.add(s2)
	stus.add(s3)

	stus.print()

	s2.name = "aa"
	stus.update(s2)
	stus.print()

	stus.delete(1)
	stus.print()
}
